package com.cwf.system.controller;


import com.cwf.common.core.controller.BaseController;
import com.cwf.common.entity.http.R;
import com.cwf.common.utils.validator.ValidatorUtil;
import com.cwf.framework.page.entity.PageParams;
import com.cwf.system.entity.Permission;
import com.cwf.system.service.PermissionService;
import com.cwf.system.validation.permission.AddPermission;
import com.cwf.system.validation.permission.PermissionGroup;
import com.cwf.system.vo.PermissionTreeVO;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@SuppressWarnings("all")
@RestController
@CrossOrigin
@RequestMapping("/system/menu")
public class PermissionController extends BaseController {

    @Autowired
    PermissionService permissionService;


    @Autowired
    RedisTemplate<String, Object> redisTemplate;


    /**
     * @return
     * @description 获取所有菜单/权限
     * @author 18068
     * @date 2023/2/16 10:12
     */
    @GetMapping("/tree/list")
    public R selectTreeList(
           @ModelAttribute Permission permission
    ) {
        return R.success(permissionService.getPermissionTreeVO(permission));
    }



    @GetMapping("/tree/list/all")
    public R<List<PermissionTreeVO>> getAllPermissionTree() {
        return R.success(permissionService.getAllPermissionTree());
    }

    @GetMapping("/info/{id}")
    public R byIdGetPermissionInfo(@PathVariable Integer id) {

        Permission permission = permissionService.byIdGetPermissionInfo(id);
        return R.success(permission);
    }


    /**
     * 添加菜单/按钮
     * @param category 类别
     * @param permission 数据
     */
    @PreAuthorize("hasAuthority('sys:menu:add')")
    @PostMapping("/add/{category}")
    public R addOne(
            @PathVariable Integer category,
            @RequestBody AddPermission permission
    ) {


        if (category == 1) {
            ValidatorUtil.check(permission, PermissionGroup.addMenu.class);
            return R.insert(permissionService.add(AddPermission.addMenu(permission)));
        } else if (category == 2) {
            ValidatorUtil.check(permission, PermissionGroup.addMark.class);
            Permission addPermission = AddPermission.addMark(permission);
            return R.insert(permissionService.add(addPermission));
        } else if (category == 3) {
            ValidatorUtil.check(permission, PermissionGroup.addButton.class);
            Permission addPermission = AddPermission.addButton(permission);
            return R.insert(permissionService.add(addPermission));
        }


        return R.success(permission);
    }


    @PreAuthorize("hasAuthority('sys:menu:update')")
    @PutMapping("/update/{id}")
    public R update(
            @PathVariable @NotNull String id,
            @RequestBody Permission permission
    ) {
        isEmpty(permission);

        Permission build = Permission.builder()
                .id(Long.valueOf(id))
                .title(permission.getTitle())
                .path(permission.getPath())
                .name(permission.getName())
                .component(permission.getComponent())
                .mark(permission.getMark())
                .icon(permission.getIcon())
                .redirect(permission.getRedirect())
                .state(permission.getState())
                .isShow(permission.getIsShow())
                .sort(permission.getSort()).build();
        boolean res = permissionService.update(build);
        return R.update(res);
    }

    /**
     * 删除菜单/按钮
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable @NotNull Integer id){
        return R.delete(permissionService.delete(id));
    }

}
